package org.scboy.rbac.controller;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.rbac.entity.AuthorizeForm;
import org.scboy.rbac.service.ResourceService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("rbac/resource")
@Menu("权限管理系统.资源管理")

public class ResourceController {
	
	@Autowired
	ResourceService  resourceService;
	
	/**
	 * 分页查询资源列表
	 * @param request
	 * @return
	 */
	@PostMapping("page")
	@Permission("分页查询资源列表")
	public Result page(@RequestBody Model params){
		Page page=resourceService.page(params);
		return Result.Success(page);
	}
	
	/**
	 * 新增资源
	 * @param request
	 * @return
	 */
	@PostMapping("add")
	@Permission("新增资源")
	public Result add(@RequestBody Model params){
		if(resourceService.add(params)){
			return Result.Success("RBAC_RESOURCE_ADD_SUCCESS");
		}else{
			return Result.Error("RBAC_RESOURCE_ADD_ERROR");
		}
	}
	
	/**
	 * 更新资源
	 * @param request
	 * @return
	 */
	@PutMapping("update")
	@Permission("更新资源")
	public Result update(@RequestBody Model params){
		if(resourceService.update(params)){
			return Result.Success("RBAC_RESOURCE_UPDATE_SUCCESS");
		}else{
			return Result.Error("RBAC_RESOURCE_UPDATE_ERROR");
		}
	}
	
	/**
	 * 删除资源
	 * @param request
	 * @return
	 */
	@DeleteMapping("delete")
	@Permission("删除资源")
	public Result delete(String id){
		if(resourceService.delete(id)){
			return Result.Success("RBAC_RESOURCE_DELETE_SUCCESS");
		}else{
			return Result.Error("RBAC_RESOURCE_DELETE_ERROR");
		}
	}
	
	/**
	 * 获取资源信息
	 * @param request
	 * @param id
	 * @return
	 */
	@GetMapping("get")
	@Permission("获取资源信息")
	public Result get(String id){
		return Result.Success(resourceService.get(id),"");
	}
	
	/**
	 * * 获取启用的资源
	 * @param request
	 * @param 
	 * @return
	 */
	@GetMapping("list")
	@Permission("获取非默认的启用资源")
	public  Result getList(){
		return Result.Success(resourceService.getList(), "");
	}
	
	/**
	 * 设置取消默认
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("isdefault")
	@Permission("设置取消默认")
	public Result isDefault(String id){
		if(resourceService.isDefault(id)){
			return Result.Success("RBAC_RESOURCE_DEFAULT_SUCCESS");
		}else{
			return Result.Error("RBAC_RESOURCE_DEFAULT_ERROR");
		}
	}

	/**
	 * 启用禁用
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("checkstate")
	@Permission("启用禁用")
	public Result checkState(String id){
		if(resourceService.checkState(id)){
			return Result.Success("RBAC_RESOURCE_CHECKSTATE_SUCCESS");
		}else{
			return Result.Error("RBAC_RESOURCE_CHECKSTATE_ERROR");
		}
	}
	
	@PostMapping("authorize")
	@Permission("资源授权")
	public Result authorize(@RequestBody AuthorizeForm params){
		
		if(resourceService.authorize(params)){
			return Result.Success("RBAC_RESOURCE_AUTHORIZE_SUCCESS");
		}else{
			return Result.Error("RBAC_RESOURCE_AUTHORIZE_ERROR");
		}
	}
	@GetMapping("loadAuthorize")
	@Permission("资源授权")
	public Result loadAuthorize(String resId){
		return Result.Success(resourceService.loadAuthorize(resId));
	}
	@GetMapping("authorizeTree")
	@Permission("获取资源权限配置树")
	public Result authorizeTree(String resId){
		return Result.Success(resourceService.getAuthorizeTree(resId));
	}

   /**
    * 获取组织关联的资源
    * @param orgId
    * @return
    */
	@GetMapping("getResourceOrg")
	@Permission("获取组织关联的资源")
	public Result getResourceOrg(String orgId){
		return Result.Success(resourceService.getResourceOrg(orgId));
	}
	   
   /**
    * 获取用户关联的资源
    * @param orgId
    * @return
    */
	@GetMapping("getResourceUser")
	@Permission("获取用户关联的资源")
	public Result getResourceUser(String userId){
		return Result.Success(resourceService.getResourceUser(userId));
	}

   /**
    * 获取组织类型关联的资源
    * @param orgId
    * @return
    */
	@GetMapping("getResourceOrgType")
	@Permission("获取组织类型关联的资源")
	public Result getResourceOrgType(String orgTypeId){
		return Result.Success(resourceService.getResourceOrgType(orgTypeId));
	}

   /**
    * 获取用户类型关联的资源
    * @param orgId
    * @return
    */
	@GetMapping("getResourceUserType")
	@Permission("获取用户类型关联的资源")
	public Result getResourceUserType(String userTypeId){
		return Result.Success(resourceService.getResourceUserType(userTypeId));
	}
	   
   /**
    * 获取角色类型关联的资源
    * @param orgId
    * @return
    */
	@GetMapping("getResourceRoleType")
	@Permission("获取角色类型关联的资源")
	public Result getResourceRoleType(String code){
		return Result.Success(resourceService.getResourceRoleType(code));
	}
	   
   /**
    * 修改组织关联的资源
    * @param orgId
    * @return
    */
	@PutMapping("updateResourceOrg")
	@Permission("修改组织关联的资源")
	public Result updateResourceOrg(@RequestBody Model params){
		String orgId=params.getString("orgId");
		String [] resId=params.getStringArray("resId");
		resourceService.updateResourceOrg(orgId,resId);
		return Result.Success("RBAC_RESOURCE_ORG_SUCCESS");
	}
	   
   /**
    * 修改用户关联的资源
    * @param orgId
    * @return
    */
	@PutMapping("updateResourceUser")
	@Permission("修改用户关联的资源")
	public Result updateResourceUser(@RequestBody Model params){
		String userId=params.getString("userId");
		String [] resId=params.getStringArray("resId");
		resourceService.updateResourceUser(userId,resId);
		return Result.Success("RBAC_RESOURCE_USER_SUCCESS");
	}
	   
   /**
    * 修改组织类型关联的资源
    * @param orgId
    * @return
    */
	@PutMapping("updateResourceOrgType")
	@Permission("修改组织类型关联的资源")
	public Result updateResourceOrgType(@RequestBody Model params){
		String orgTypeId=params.getString("orgTypeId");
		String [] resId=params.getStringArray("resId");
		resourceService.updateResourceOrgType(orgTypeId,resId);
		return Result.Success("RBAC_RESOURCE_OT_SUCCESS");
	}
	   
   /**
    * 修改用户类型关联的资源
    * @param orgId
    * @return
    */
	@PutMapping("updateResourceUserType")
	@Permission("修改用户类型关联的资源")
	public Result updateResourceUserType(@RequestBody Model params){
		String userTypeId=params.getString("userTypeId");
		String[] resId=params.getStringArray("resId");
		resourceService.updateResourceUserType(userTypeId,resId);
		return Result.Success("RBAC_RESOURCE_UT_SUCCESS");
	}
	   
   /**
    * 修改角色类型关联的资源
    * @param orgId
    * @return
    */
	@PutMapping("updateResourceRoleType")
	@Permission("修改角色类型关联的资源")
	public Result updateResourceRoleType(@RequestBody Model params){
		String code=params.getString("code");
		String [] resId=params.getStringArray("resId");
		resourceService.updateResourceRoleType(code,resId);
		return Result.Success("RBAC_RESOURCE_RT_SUCCESS");
	}
	
	
	@PostMapping("resourceOrgPage")
	@Permission("分页获取组织关联的资源")
	public Result resourceOrgPage(@RequestBody Model params) {
		return Result.Success(resourceService.resourceOrgPage(params));
	}
	
	@PostMapping("resourceUserPage")
	@Permission("分页获取用户关联的资源")
	public Result  resourceUserPage(@RequestBody Model params) {
		return Result.Success(resourceService.resourceUserPage(params));
	}

	
	@PostMapping("resourceOrgTypePage")
	@Permission("分页获取组织类型关联的资源")
	public Result  resourceOrgTypePage(@RequestBody Model params) {
		return Result.Success(resourceService.resourceOrgTypePage(params));
	}

	
	@PostMapping("resourceUserTypePage")
	@Permission("分页获取用户类型关联的资源")
	public Result  resourceUserTypePage(@RequestBody Model params) {
		return Result.Success(resourceService.resourceUserTypePage(params));
	}
	
	@PostMapping("resourceRoleTypePage")
	@Permission("分页获取角色类型关联的资源")
	public Result  resourceRoleTypePage(@RequestBody Model params) {
		return Result.Success(resourceService.resourceRoleTypePage(params));
	}
	
	@DeleteMapping("removeResourceOrg")
	@Permission("移除组织关联的资源")
	public Result removeResourceOrg(String id){
		if(resourceService.removeResourceOrg(id)){
			return Result.Success("RBAC_ORG_RESOURCE_REMOVE_SUCCESS");
		}else{
			return Result.Error("RBAC_ORG_RESOURCE_REMOVE_ERROR");
		}
	}
	@DeleteMapping("removeResourceUser")
	@Permission("移除用户关联的资源")
	public Result removeResourceUser(String id){
		if(resourceService.removeResourceUser(id)){
			return Result.Success("RBAC_USER_RESOURCE_REMOVE_SUCCESS");
		}else{
			return Result.Error("RBAC_USER_RESOURCE_REMOVE_ERROR");
		}
	}
	
	@DeleteMapping("removeResourceOrgType")
	@Permission("移除组织类型关联的资源")
	public Result removeResourceOrgType(String id){
		if(resourceService.removeResourceOrgType(id)){
			return Result.Success("RBAC_RESOURCE_ORGTYPE_DELETE_SUCCESS");
		}else{
			return Result.Error("RBAC_RESOURCE_ORGTYPE_DELETE_ERROR");
		}
	}

	@DeleteMapping("removeResourceUserType")
	@Permission("移除用户类型关联的资源")
	public Result removeResourceUserType(String id){
		if(resourceService.removeResourceUserType(id)){
			return Result.Success("RBAC_RESOURCE_USERTYPE_DELETE_SUCCESS");
		}else{
			return Result.Error("RBAC_RESOURCE_USERTYPE_DELETE_ERROR");
		}
	}
	   
	@DeleteMapping("removeResourceRoleType")
	@Permission("移除角色类型关联的资源")
	public Result removeResourceRoleType(String id){
		if(resourceService.removeResourceRoleType(id)){
			return Result.Success("RBAC_RESOURCE_ROLETYPE_DELETE_SUCCESS");
		}else{
			return Result.Error("RBAC_RESOURCE_ROLETYPE_DELETE_ERROR");
		}
	}
}
